package com.dba.example.gwt.client.activities;

import javax.inject.Inject;

import com.dba.example.gwt.client.beans.Company;
import com.dba.example.gwt.client.beans.Page;
import com.dba.example.gwt.client.commands.CompanyPageCallback;
import com.dba.example.gwt.client.commands.ExampleCommandFactory;
import com.dba.example.gwt.client.places.CompanyListPlace;
import com.dba.example.gwt.client.views.CompanyListView;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class CompanyListActivity extends AbstractActivity implements
		CompanyListView.Presenter {
	
	ExampleCommandFactory cf;

	CompanyListView view;

	PlaceController pc;

	@SuppressWarnings("unused")
	private CompanyListPlace place;

	@Inject
	public CompanyListActivity(CompanyListView view, PlaceController pc, ExampleCommandFactory cf) {
		this.view = view;
		this.pc = pc;
		this.cf = cf;
	}

	public void setPlace(CompanyListPlace place) {
		this.place = place;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
		
		view.reset();
	}
	
	@Override
	public void goTo(Place place) {
		pc.goTo(place);
	}

	@Override
	public void getPage(int offset, int size, String sortColumn, String sortDirection, String search) {
		Command c = cf.companyPage(offset, size, sortColumn, sortDirection, search, new CompanyPageCallback() {
			public void success(Page<Company> page) {
				view.setPage(page);
			}
			
			@Override
			public void error(String error) {
				Window.alert(error);
			}
		});
		c.execute();
	}
	
	public void onStop() {
	}

}
